function changeName(){
    var check;
    var username = document.getElementById("username").value;
    if(username.length == 0){
        document.getElementById("username").focus;
        check = false;
    }else{
        check = true;
    }
    return check;
}

function changeCardID() {
    var check;
    var CardID = document.getElementById("CardID").value;
    var reCardID = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    if (CardID.length !== 18 || !reCardID.test(CardID)) {
        document.getElementById("tip1").innerHTML = "*身份证格式有误 x";
        document.getElementById("tip1").style.color = "#FF0000";
        document.getElementById("CardID").focus;
        check = false;
    } else {
        document.getElementById("tip1").innerHTML = "*通过 √";
        document.getElementById("tip1").style.color = "#008000";
        check = true;
    }
    return check;
}

function changeNum() {
    var check;
    var numPhone = document.getElementById("numPhone").value;
    var reNum = /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/;
    if (numPhone.length !== 11 || !reNum.test(numPhone)) {
        document.getElementById("tip2").innerHTML = "*手机号格式有误 x";
        document.getElementById("tip2").style.color = "#FF0000";
        document.getElementById("numPhone").focus;
        check = false;
    } else {
        document.getElementById("tip2").innerHTML = "*通过 √";
        document.getElementById("tip2").style.color = "#008000";
        check = true;
    }
    return check;
}

function changepwd() {
    var check;
    var pwd = document.getElementById("password").value;
    var rePwd = /^([!#$%^&*(){}（）【】；‘’，。/？》《<>,/;':"]+)$/;
    if (pwd.length < 6 || pwd.length > 18 || rePwd.test(pwd)) {
        document.getElementById("tip3").innerHTML = "*密码格式有误 x";
        document.getElementById("tip3").style.color = "#FF0000";
        document.getElementById("password").focus;
        check = false;
    } else {
        document.getElementById("tip3").innerHTML = "*通过 √";
        document.getElementById("tip3").style.color = "#008000";
        check = true;
    }
    return check;
}

function changepwdc() {
    var check;
    var pwd = document.getElementById("password").value;
    var pwdc = document.getElementById("re_enter").value;
    if (pwdc != pwd) {
        document.getElementById("tip4").innerHTML = "*两次密码不一致 x";
        document.getElementById("tip4").style.color = "#FF0000";
        document.getElementById("re_enter").focus;
        check = false;
    } else {
        document.getElementById("tip4").innerHTML = "*通过 √";
        document.getElementById("tip4").style.color = "#008000";
        check = true;
    }
    return check;
}

function changeCarID(){
    var check;
    var CarID = document.getElementById("CarID").value;
    if(CarID.length == 0){
        document.getElementById("CarID").focus;
        check = false;
    }else{
        check = true;
    }
    return check;
}

function changehomeAddress(){
    var check;
    var homeAddress = document.getElementById("homeAddress").value;
    if(homeAddress.length == 0){
        document.getElementById("homeAddress").focus;
        check = false;
    }else{
        check = true;
    }
    return check;
}

function changeroomID(){
    var check;
    var roomID = document.getElementById("roomID").value;
    if(roomID.length == 0){
        document.getElementById("roomID").focus;
        check = false;
    }else{
        check = true;
    }
    return check;
}

function check(){
    var check = changeName() && changeCardID() && changeNum() && changepwd() && changepwdc() && changeCarID() && changehomeAddress() && changeroomID();
    console.log(check);
    if(check === true){
        alert("注册成功,点击确认,3秒后自动跳转");
        setTimeout(() => {
            location.href = "./views/user.html";
        },3000);
    }else{
        document.getElementById("tip1").innerHTML = "*身份证格式有误 x";
        document.getElementById("tip1").style.color = "#FF0000";
        document.getElementById("tip2").innerHTML = "*手机号格式有误 x";
        document.getElementById("tip2").style.color = "#FF0000";
        document.getElementById("tip3").innerHTML = "*密码格式有误 x";
        document.getElementById("tip3").style.color = "#FF0000";
        document.getElementById("tip4").innerHTML = "两次密码不一致 x";
        document.getElementById("tip4").style.color = "#FF0000";
    }
}



// 注册修改
function getParams(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = location.search.substring(1).match(reg);
    console.log(reg,r);
    if (r != null) {
        return decodeURIComponent(r[2]);
    }
    return null;
}

$(function () {
    let id = getParams('id');
    console.log(id);
    if (id) {
        $.ajax({
            type:'get',
            url:'http://localhost:5009/api/UserInfo/Find?id='+id,
            success:function(res){
                console.log(res[0]);
                $('[name=username]').val(res[0].name);
                $('[name=sex]').val(res[0].sex);
                $('[name=CardID]').val(res[0].idNumber);
                $('[name=num]').val(res[0].phone);
                $('[name=pwd]').val(res[0].password);
                $('[name=CarID]').val(res[0].license);
                $('[name=homeAddress]').val(res[0].home);
                $('[name=roomID]').val(res[0].room);
            }
        })
    }
})

function btnSave() {
    let id = getParams('id');
    let name = $('[name = username]').val();
    let sex = $('[name = sex]').val();
    let idNumber = $('[name = CardID]').val();
    let phone = $('[name=num]').val();
    let password = $('[name=pwd]').val();
    let pwdc = $('[name=pwdc]').val();
    let license = $('[name=CarID]').val();
    let home = $('[name=homeAddress]').val();
    let room = $('[name=roomID]').val();
    console.log(id);
    if (id) {
        console.log("修改");
        $.ajax({
            type:'put',
            url:'http://localhost:5009/api/UserInfo/Update/'+id,
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify({ name, sex, idNumber, phone, password, home, license, room }),
            success:function(res){
                console.log(res);
                alert("修改成功");
                location.href = './index.html'
            }
        })
    } else {
        console.log("注册");
        if (password === pwdc) {
            $.ajax({
                type: 'post',
                url: 'http://localhost:5009/api/UserInfo/Add',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify({ name, sex, idNumber, phone, password, home, license, room }),
                success: function (res) {
                    console.log(res);
                    alert("注册成功")
                    location.href = './login.html'
                }
            })
           } else {
            console.log("两次密码不一样，添加失败");
            alert("两次密码不一样，添加失败")
           }
    }
}